package com.dmcloud.common.util.auth;

import com.dmcloud.common.constant.SecurityConstants;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * Created by xiaodao
 * date: 2020/5/14
 */
public class JwtUtil {
    public static String BASE64_SECURITY;
    public static String getToken(String auth) {
        if (auth != null && auth.length() > 7) {
            String headStr = auth.substring(0, 6).toLowerCase();
            if (headStr.compareTo("bearer") == 0) {
                auth = auth.substring(7);
            }

            return auth;
        } else {
            return null;
        }
    }
    public static Claims parseJWT(String jsonWebToken) {
        try {
            BASE64_SECURITY =    Base64.getEncoder().encodeToString(SecurityConstants.JWT_SIGN_KEY.getBytes(StandardCharsets.UTF_8));
            return (Claims) Jwts.parser().setSigningKey(Base64.getDecoder().decode(BASE64_SECURITY)).parseClaimsJws(jsonWebToken).getBody();
        } catch (Exception var2) {
            return null;
        }
    }
}
